package com.v1.web.taglib;

import java.io.IOException;
import java.text.MessageFormat;

import javax.servlet.ServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.Tag;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.lang.StringUtils;

import com.v1.model.BaseModel;

public class PageNavigator extends TagSupport {

	private static final long serialVersionUID = 1L;

	protected String name;

	private String onclick;

	private String href = "#";

	public PageNavigator() {
	}

	@Override
	public int doEndTag() throws JspException {
		return Tag.EVAL_PAGE;
	}

	@Override
	public int doStartTag() throws JspException {

		if (StringUtils.isEmpty(this.name)) {
			return Tag.SKIP_BODY;
		}

		ServletRequest request = pageContext.getRequest();
		BaseModel navigator = (BaseModel) request.getAttribute(this.name);
		if (navigator == null || navigator.getRowCount() <= 0) {
			return SKIP_BODY;
		}

		StringBuffer pn = new StringBuffer(150);
		String format_nonactivated = getResourceMessage("nonactivated");
		String separator = getResourceMessage("separator");
		int pageNo = navigator.getPageNo();
		// Previous page
		if (pageNo > 1) {
			formatNonactived(pn, format_nonactivated, 1, getResourceMessage("first"));
			pn.append("&nbsp;");
			formatNonactived(pn, format_nonactivated, pageNo - 1, getResourceMessage("previous"));
		}

		// pages
		String format_activated = getResourceMessage("activated");
		int pageCount = navigator.getPageCount();
		int pageIndexCount = navigator.getPageIndexCount();
		int lowerBound = Math.max(1, pageNo - 5);
		int upperBound = Math.min(lowerBound + pageIndexCount - 1, pageCount);
		for (int i = lowerBound; i <= upperBound; i++) {
			if (i == pageNo) {
				pn.append(MessageFormat.format(format_activated, pageNo));
			} else {
				formatNonactived(pn, format_nonactivated, i);
			}
			pn.append(separator);
		}

		// next page
		if (pageNo < pageCount) {
			formatNonactived(pn, format_nonactivated, pageNo + 1, getResourceMessage("next"));
			pn.append("&nbsp;");
			formatNonactived(pn, format_nonactivated, pageCount, getResourceMessage("last"));
		}

		pn.append("&nbsp;");
		pn.append("&nbsp;");
		pn.append(MessageFormat.format(format_activated, "共" + pageCount + "页"));

		String format_navigator = getResourceMessage("navigator");
		String html = MessageFormat.format(format_navigator, pn.toString());
		try {
			pageContext.getOut().write(html);
		} catch (IOException e) {
			e.printStackTrace();
			throw new JspException(e);
		}
		return Tag.EVAL_BODY_INCLUDE;
	}

	private void formatNonactived(StringBuffer pn, String format_nonactivated, int no, String label) {
		String t = no + "";
		String attribute_href = MessageFormat.format(href, t);

		String attribute_onclick = "";
		if (StringUtils.isNotEmpty(onclick)) {
			attribute_onclick = MessageFormat.format(onclick, t);
		}
		String html = MessageFormat.format(format_nonactivated, new Object[] { label, attribute_href,
				attribute_onclick });
		pn.append(html);
	}

	private void formatNonactived(StringBuffer pn, String format_nonactivated, int no) {
		formatNonactived(pn, format_nonactivated, no, "" + no);
	}

	private String getResourceMessage(String key) {
		if ("separator".equals(key)) {
			return "\n";
		} else if ("previous".equals(key)) {
			return "上一页";
		} else if ("first".equals(key)) {
			return "首页";
		} else if ("next".equals(key)) {
			return "下一页";
		} else if ("last".equals(key)) {
			return "末页";
		} else if ("nonactivated".equals(key)) {
			return "<a href=\"{1}\" onclick=\"{2}\"><span>{0}</span></a>";
		} else if ("activated".equals(key)) {
			return "<span>{0}</span>";
		} else if ("navigator".equals(key)) {
			return "<div class=\"comments-pagination\">{0}</div>";
		}
		return null;
	}

	public String getOnclick() {
		return onclick;
	}

	public void setOnclick(String onclick) {
		this.onclick = onclick;
	}

	public String getHref() {
		return href;
	}

	public void setHref(String href) {
		this.href = href;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}
